package mipo.top.shop.controller;

import mipo.top.admin.domain.Employee;
import mipo.top.admin.service.IEmployeeService;
import mipo.top.base.util.JsonResult;
import mipo.top.base.util.LoginContext;
import mipo.top.base.util.PageUtil;
import mipo.top.shop.Dto.AcceptDto;
import mipo.top.shop.domain.SearchMasterMsg;
import mipo.top.shop.query.SearchMasterMsgQuery;
import mipo.top.shop.service.ISearchMasterMsgService;
import mipo.top.users.domain.Logininfo;
import mipo.top.users.domain.User;
import mipo.top.users.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IUserService userService;

    @GetMapping
    public List<SearchMasterMsg> sad(){
        return searchMasterMsgService.findall();
    }

    @PutMapping
    public JsonResult su(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if(searchMasterMsg.getId()==null){
                searchMasterMsgService.save(searchMasterMsg);
            }else{
                searchMasterMsgService.update(searchMasterMsg);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("错误数据");
        }
    }

    @DeleteMapping("/{id}")
    public void adw(@PathVariable("id") Long id){
        searchMasterMsgService.remove(id);
    }

    @PostMapping
    public PageUtil<SearchMasterMsg> asp(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery){
        return searchMasterMsgService.queryData(searchMasterMsgQuery);
    }
    @PostMapping("/publish")
    public JsonResult publish(@RequestBody SearchMasterMsg searchMasterMsg , HttpServletRequest request){
        try {
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Long id = loginUser.getId();
            User user = userService.findlogininfoId(id);
            searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @PatchMapping
    private void bacthDel(@RequestBody List<Long> ids){
        searchMasterMsgService.patchremove(ids);
    }

    @PostMapping("/toAudit")
    public PageUtil<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery){
        searchMasterMsgQuery.setState(0);
        return searchMasterMsgService.queryByStateData(searchMasterMsgQuery);
    }

    @PostMapping("/toHandle")
    public PageUtil<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request){
        try{
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Employee employee = employeeService.findBylogininfoId(loginUser.getId());
            searchMasterMsgQuery.setState(1);
            searchMasterMsgQuery.setShopId(employee.getShopId());
            return searchMasterMsgService.queryByStateData(searchMasterMsgQuery);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/pool")
    public PageUtil<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery){
        searchMasterMsgQuery.setState(1);
        return searchMasterMsgService.queryByStateData(searchMasterMsgQuery);
    }
    @GetMapping("/takePet/{id}")
    public JsonResult takePet(@PathVariable("id") Long id ,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLoginUser(request);
            Employee employee = employeeService.findBylogininfoId(logininfo.getId());
            searchMasterMsgService.takePet(id,employee);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }

    @PostMapping("/finish")
    public PageUtil<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery){
        searchMasterMsgQuery.setState(2);
        return searchMasterMsgService.queryByStateData(searchMasterMsgQuery);
    }
    @PostMapping("/user")
    public PageUtil<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request){
        try {
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Employee employee = employeeService.findBylogininfoId(loginUser.getId());
            searchMasterMsgQuery.setShopId(employee.getShopId());
            return searchMasterMsgService.queryByStateData(searchMasterMsgQuery);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/reject/{id}")
    private JsonResult reject(@PathVariable("id") Long id , HttpServletRequest request){
        try{
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Employee employee = employeeService.findBylogininfoId(loginUser.getId());
            return searchMasterMsgService.reject(id,employee);
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }

    @PostMapping("/accept")
    private JsonResult accept(@RequestBody AcceptDto acceptDto){
        searchMasterMsgService.accept(acceptDto);
        return JsonResult.me();
    }
}